# -- coding: utf-8 --

# 该模块管理行政综合业务模块的采购登记、领用登记和归还记录页面
import time
import re
from wnboss05.tools.ui import UI
from wnboss05.tools.util import  FileParser, DBParser


class PurchaseRegistration:

    # 点击新增设备按钮
    def click_add_assets_button(self, locators):
        UI.click(locators['add_assets_button'])

    # 选择要添加的设备名称
    def select_assets_name(self, locators):
        UI.select_option(locators['assets_name'])

    # 选择任意的设备类型
    def select_assets_type(self, locators):
        UI.select_option(locators['assets_type'])

    # 输入条码
    def input_barcode(self, locators, add_assets_data):
        UI.input(locators['barcode'], add_assets_data['barcode'])

    # 输入采购价
    def input_price(self, locators, add_assets_data):
        UI.input(locators['price'], add_assets_data['price'])

    # 输入归属人
    def input_employee(self, locators, add_assets_data):
        UI.input(locators['employee'], add_assets_data['employee'])
        time.sleep(1)
        UI.click(locators['employee_select'])

    # 选择采购时间
    def select_time(self, locators):
        UI.click(locators['calendar'])
        UI.click(locators['time'])

    # 添加采购备注
    def input_note(self, locators, add_assets_data):
        UI.input(locators['note'], add_assets_data['note'])

    # 选择归属
    def select_owner(self, locators):
        UI.select_option(locators['owner'])

    # 点击保存按钮
    def click_save_button(self, locators):
        UI.click(locators['assets_save'])

    # 输入查询条码
    def input_query_barcode(self, locators, query_assets_data):
        UI.input(locators['query_barcode'], query_assets_data['barcode'])

    # 点击查询按钮
    def click_query_button(self, locators):
        UI.click(locators['query_barcode_button'])

    # 判断列表信息是否存在
    def exists_list_info(self, locators):
        return UI.exists(locators['assets_list_info'])

    # 获取列表信息中的记录总数
    def get_list_count(self, locators):
        info = UI.get_element(locators['assets_list_info']).text

        return re.findall('总共 (.*?) 条记录', info)

    # 执行测试动作
    def do_add_assets(self, add_assets_data):
        locators = FileParser.read_json('../locators/ai.json')['pr']
        sql = 'SELECT count(assets_id) from assets'
        old = DBParser.query('app', sql)  # 获得没有添加设置之前的记录数

        self.click_add_assets_button(locators)  # 点击新增
        self.select_assets_name(locators)
        self.select_assets_type(locators)
        self.input_barcode(locators, add_assets_data)
        self.input_price(locators, add_assets_data)
        self.input_employee(locators, add_assets_data)
        self.select_time(locators)
        self.input_note(locators, add_assets_data)
        self.select_owner(locators)
        self.click_save_button(locators)
        time.sleep(2)
        if UI.exists(locators['alert_button']):  # 如果结果为真，表示有弹出窗口
            UI.refresh()

        # 获取测试结果
        new = DBParser.query('app', sql)  # 获得添加设备之后的记录数
        if new[0][0] - old[0][0] == 1:
            actual = '添加成功'
        else:
            actual = '添加失败'

        return actual

    def do_query_assets(self, query_assets_data):
        locators = FileParser.read_json('../locators/ai.json')['pr']
        self.input_query_barcode(locators, query_assets_data)
        self.click_query_button(locators)
        time.sleep(3)

        sql = 'select count(assets_id) from assets'  # 从数据库assets表中获取总记录数
        db_count = DBParser.query('app', sql)[0][0]
        # 页面上的这个元素因为不同的条件会被隐藏，但可以查询到
        list_count = self.get_list_count(locators)
        if len(list_count) == 0:
            actual = '没有查询到设备'
        elif list_count[0] == '1':
            actual = '查询到一条'
        elif list_count[0] == str(db_count):
            actual = '查询到全部设备'
        else:
            actual = '查询结果不正确'
        return actual

    def modify_asserts(self):
        pass


class CollectionRegistration:

    # 点击新增
    def click_add_button(self, locators):

        UI.click(locators['add_record_button'])

    # 输入条码
    def input_barcode(self, locators, add_record_data):
        UI.input(locators['barcode'], add_record_data['barcode'])

    # 选择归属人类型
    def select_owner_type(self, locators):
        UI.select_option(locators['owner_type'])

    # 选择领用人
    def select_borrower(self, locators, add_record_data):
        UI.input(locators['borrower'], add_record_data['borrower'])
        time.sleep(2)
        UI.click(locators['borrower_window'])

    # 添加备注
    def add_note(self, locators, add_record_data):
        UI.input(locators['note'], add_record_data['note'])

    # 点击保存按钮
    def click_save_button(self, locators):
        UI.click(locators['save_button'])

    # 点击错误提示弹窗的确认按钮
    def click_confirm_button(self, locators):
        UI.click(locators['confirm_button'])
        UI.refresh()

    # 获取列表中的记录数
    def get_list_record_count(self, locators):
        info = UI.get_element(locators['list_info']).text
        return re.findall('总共 (.*?) 条记录', info)

    # 点击归还按钮
    def click_back_button(self, locators):
        UI.click(locators['back_button'])

    # 点击确认归还按钮
    def click_confirm_back_button(self, locators):
        UI.click(locators['confirm_back_button'])
        UI.refresh()

    # 执行添加归还记录的动作
    def do_add_back_record(self, add_record_data):

        locators = FileParser.read_json('../locators/ai.json')['cr']
        old = self.get_list_record_count(locators)
        self.click_add_button(locators)
        self.input_barcode(locators, add_record_data)
        self.select_owner_type(locators)
        self.select_borrower(locators, add_record_data)
        self.add_note(locators, add_record_data)
        self.click_save_button(locators)
        if UI.exists(locators['confirm_button']):
            self.click_confirm_button(locators)
            actual = '添加失败'
        time.sleep(2)
        new = self.get_list_record_count(locators)
        if old[0] != new[0]:
            actual = '添加成功'
        else:
            actual = '添加失败'
        return actual

    # 执行归还动作
    def do_back(self):
        locators = FileParser.read_json('../locators/ai.json')['cr']
        old = self.get_list_record_count(locators)
        self.click_back_button(locators)
        self.click_confirm_back_button(locators)
        time.sleep(2)
        new = self.get_list_record_count(locators)
        if old[0] != new[0]:
            actual = '归还成功'
        else:
            actual = '归还失败'

class BackRecord:
    pass

